package com.solace.selenium;

import com.solace.selenium.service.WebDriverService;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.io.File;

@SpringBootApplication
public class SeleniumApplication extends Application {
    public  static ConfigurableApplicationContext applicationContext;
    public static void main(String[] args) {
        applicationContext = SpringApplication.run(SeleniumApplication.class, args);
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        //容器的根节点设置为边缘布局
        BorderPane root = new BorderPane();

        //水平布局
        HBox hbox = new HBox();
        //设置hbox布局方式为居中
        hbox.setAlignment(Pos.CENTER);
        //padding属性可设置为管理节点与HBox窗格边缘之间的距离,分别是上，右，下，左
        hbox.setPadding(new Insets(20,10,20,10));
        //设置节点之间的间距，这里设置的是TextField、Button之间水平间距
        hbox.setSpacing(2);
        //设置背景颜色
        hbox.setStyle("-fx-background-color: #4A525AFF;");
        //选择导出文件夹路径
        TextField textField = new TextField();
        Button selectButton = new Button("选择文件");
        selectButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                DirectoryChooser dc = new DirectoryChooser();
                dc.setInitialDirectory(new File("D:" + File.separator));
                File file = dc.showDialog(primaryStage);
                if (file!=null) {
                    String path = file.getPath();
                    System.out.println("选择的文件夹:"+path);
                    textField.setText(path);
                }
            }
        });

        //清除页面星号
        Button clearAsterisk = new Button("清除星号");
        clearAsterisk.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                WebDriverService webDriverService = (WebDriverService)applicationContext.getBean("webDriverServiceImpl");
                webDriverService.findXpath("//div[@class='index_tableRow__tpbkM mortise-rich-table-row']");
            }
        });
        Button exportOrder = new Button("导出订单");
        exportOrder.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                WebDriverService webDriverService = (WebDriverService)applicationContext.getBean("webDriverServiceImpl");
                String msg = webDriverService.exportOrderList(textField.getText());
                Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
                alert.setTitle("提示");
                alert.setHeaderText(null);
                alert.setContentText(msg);
                alert.show();
            }
        });
        hbox.getChildren().addAll(textField,selectButton,clearAsterisk,exportOrder);

        //Hgrow是 horizontal grow缩写意为水平增长，在这里是水平增长沾满窗口
        HBox.setHgrow(textField, Priority.ALWAYS);
        ImageView imageView = new ImageView();
        Image image = new Image("img/img.png");
        imageView.setImage(image);
        //将hbox布局容器放入BorderPane布局容器中
        root.setTop(hbox);
        //创建场景
        Scene scene = new Scene(root, 600, 60);
        primaryStage.setScene(scene);
//       scene.getStylesheets().add(HelloWorld.class.getResource("img.css").toExternalForm());

        //root.getChildren().add(imageView);

        //设置标题
        primaryStage.setTitle("订单下载工具");
        primaryStage.getIcons().add(new Image("img/am.jpeg"));
        //打开窗口
        primaryStage.show();
    }

}
